<?php

/*-----------------------------------------------------------*\
|                  - Загальні функції -                      |
|                 Author:  Skalden Russell                   |
|                 Last modify: 20130105                      |
\*-----------------------------------------------------------*/

if(!defined('TSUE_SEC')) {
	if(!defined('TSUE_DIR')){
		header('Content-Type: text/html; charset=utf-8');
		$path = dirname(__FILE__);
		$path = substr($path, 0, strlen($path)-15);
		require_once( $path.'/data/language/ru_loc/ru_adminpanel.lng');
	}else{
		$path = TSUE_DIR;
	}
	require_once( $path.'/data/messages/hacking.attempt.php');
	die($TSUE_MSG);
}

function check_for_install( $root_dir ) 
{
	if( @file_exists( $root_dir.'/data/installed.php')) 
		return true;
	else
		return false;
}

function make_splitter( $current_page, $total, $news_per_page, $getquery = '' )
{
	
	$splitter = "";
	if(empty($current_page))
	{
		$current_page = 1;
	}
	
	if($news_per_page == 0)
	{
		return '';
	}
	
	$begin = ($current_page - 1) * $news_per_page;
	$number = (int)($total / $news_per_page);
	if( ((float)( $total / $news_per_page ) - $number) != 0) $number++; // Округление
			
	// смуга роздільник
	$splitter = '';
	
	if($total > $news_per_page)
	{
		$splitter .= '<div class="news_navigation" align="center" style="margin:0px 0px 20px 0px;">';
		
		if(empty($current_page) || $current_page == 1)
		{
			$splitter .= '&nbsp;<span>Назад</span>&nbsp;';
		}
		else 
		{
			$splitter .= '&nbsp;<a href="'. $getquery . ($current_page-1) .'"> Назад </a>&nbsp;';
		}
		
		if($number >= 11)
		{
			for($i = 0; $i < 3; $i++)
			{
				if($i == ($current_page-1))
				{
					$splitter .= '&nbsp;<span>'. ($i+1) .'</span>&nbsp;';
				}
				else
				{
					$splitter .= '&nbsp;<a href="'. $getquery . ($i+1) .'">'. ($i+1) .'</a>&nbsp;';
				}
			}
			
			$splitter .= '&nbsp;...&nbsp;';
			
			if($current_page <= 3)
			{	
				$from = 3; $to = 5; 
			}
			else if($current_page >= ($number - 3))
			{
				$from = $number - 6; $to = $number - 4;
			}
			else
			{
				$from = $current_page - 2; $to = $current_page ;
			}
			
			for($i = $from; $i <= $to; $i++)
			{
				if($i == ($current_page-1))
				{
					$splitter .= '&nbsp;<span>'. ($i+1) .'</span>&nbsp;';
				}
				else
				{
					$splitter .= '&nbsp;<a href="'. $getquery . ($i+1) .'">'. ($i+1) .'</a>&nbsp;';
				}
			}
	
			
			$splitter .= '&nbsp;...&nbsp;';
			
			for($i = $number - 3; $i < $number; $i++)
			{
				if($i == ($current_page-1))
				{
					$splitter .= '&nbsp;<span>'. ($i+1) .'</span>&nbsp;';
				}
				else
				{
					$splitter .= '&nbsp;<a href="'. $getquery . ($i+1) .'">'. ($i+1) .'</a>&nbsp;';
				}
			}
		}
		else
		{
			for($i = 0; $i < $number; $i++)
			{
				if($i == ($current_page-1))
				{
					$splitter .= '&nbsp;<span>'. ($i+1) .'</span>&nbsp;';
				}
				else
				{
					$splitter .= '&nbsp;<a href="'. $getquery . ($i+1) .'">'. ($i+1) .'</a>&nbsp;';
				}
			}
		}
		
		if($number == $current_page)
		{
			$splitter .= '&nbsp;<span>Вперёд</span>&nbsp;';
		}
		else
		{
			$splitter .= '&nbsp;<a href="'. $getquery . ($current_page+1) .'"> Вперёд </a>&nbsp;';
		}
		
		$splitter .= '</div>';
	}
	
	$content = array();
	$content['splitter'] = $splitter;
	$content['begin'] = $begin;
	
	return $content;
}

// Функція GZIP compression
function print_gzipped_page(  ) 
{
    $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
    if( headers_sent() )
        $encoding = false;
    else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
        $encoding = 'x-gzip';
    else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
        $encoding = 'gzip';
    else
        $encoding = false;

   if( $encoding ) 
   {
        $contents = ob_get_clean();
        header('Content-Encoding: '.$encoding);
        print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
        $contents = gzcompress($contents, 9);
        $contents = substr($contents, 0, strlen($contents));
        print($contents);
    }
    else 
	{
        ob_end_flush();

	}
}

?>